package com.dawn.trademanage.dao;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.dawn.trademanage.bean.TradeDetailBean;
import com.dawn.trademanage.bean.TradeHistoryBean;
import com.dawn.trademanage.bean.TradeInfoBean;

@Repository
public class TradeManageDao {

	@Autowired
	private HibernateTemplate hibernateTemplate;

	public void addTrade(TradeInfoBean tradeInfo) {
		hibernateTemplate.save(tradeInfo);
	}

	@SuppressWarnings("unchecked")
	public List<TradeInfoBean> showTradeList() {
		List<TradeInfoBean> tradeList;
		tradeList = (List<TradeInfoBean>) hibernateTemplate.find(
				"from TradeInfoBean ti where ti.isStop=?", (byte) 0);
		return tradeList;
	}

	public TradeInfoBean findTradeById(long tradeId) {
		TradeInfoBean tradeInfo = hibernateTemplate.get(TradeInfoBean.class,
				tradeId);
		return tradeInfo;
	}

	public void updateTradeInfo(TradeInfoBean tradeInfoBean) {
		hibernateTemplate.update(tradeInfoBean);
	}

	@SuppressWarnings("unchecked")
	public boolean findIsFollowed(long userid, long tradeid) {
		List<TradeDetailBean> list = (List<TradeDetailBean>) hibernateTemplate
				.find("from TradeDetailBean td where td.fansId = ? and td.ttTradeinfo.tradeId = ?",
						userid, tradeid);
		if (null == list || list.size() == 0)
			return false;
		return true;
	}

	public void addTradeDetail(TradeDetailBean tradeDetailBean) {
		hibernateTemplate.save(tradeDetailBean);
	}

	public void addTradeHistory(TradeHistoryBean tradeHistory) {
		hibernateTemplate.save(tradeHistory);
	}

	@SuppressWarnings("unchecked")
	public List<TradeDetailBean> listFollowers(long tradeId, String startTime,
			String endTime) {
		String sql = "from TradeDetailBean td where (td.followedTime >= '"
				+ startTime + "' and td.followedTime <= '" + endTime
				+ "') and td.ttTradeinfo.tradeId = " + tradeId;
		List<TradeDetailBean> list = (List<TradeDetailBean>) hibernateTemplate
				.find(sql);
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<TradeDetailBean> listIdols(long userId, String startTime,
			String endTime) {
		String sql = "from TradeDetailBean td where (td.followedTime >= '"
				+ startTime + "' and td.followedTime <= '" + endTime
				+ "') and td.fansId = " + userId;
		List<TradeDetailBean> list = (List<TradeDetailBean>) hibernateTemplate
				.find(sql);
		return list;
	}
}
